package com.explorati.LeetCode167.twosum2;

/**
 * /** 167. Two Sum II - Input array is sorted 解法三：使用双指针
 * 
 * @author explorati
 *
 */
public class Solution2 {
	public static int[] twoSum(int[] numbers, int target) {
		int[] arr = new int[2];
		int i = 0, j = numbers.length - 1;
		while (i < j) {
			if (numbers[i] + numbers[j] > target) {
				j--;
			} else if (numbers[i] + numbers[j] < target) {
				i++;
			} else {
				arr[0] = i + 1;
				arr[1] = j + 1;
				return arr;
			}
		}

		throw new IllegalArgumentException("The input has no solution.");
	}

	public static void main(String[] args) {
		int[] numbers = { 2, 7, 11, 19 };
		int[] arr = twoSum(numbers, 9);
		for (int i : arr) {
			System.out.print(i + "  ");
		}
	}
}
